home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
UTILITY1
/
MSWSRC35.ZIP
/
PRINTER.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-12
|
23KB
|
817 lines
// ObjectWindows - (C) Copyright 1992 by Borland International
//#include <windows.h>
//#include <owl.h>
//#include "printer.h"
//#include "logorc.h"
#include "allwind.h"
#include "mycombo.h"
#include <static.h>
#include <safepool.h>
// A few private constants
#define SR_ON 32512
#define SR_ERRORTEMPLATE 32513
#define SR_OUTOFMEMORY 32514
#define SR_OUTOFDISK 32515
#define SR_PRNCANCEL 32516
#define SR_PRNMGRABORT 32517
#define SR_GENERROR 32518
#define SR_ERRORCAPTION 32519
static BOOL UserAbort = FALSE;
/*
** FetchStr
** Returns a pointer to the first comma delimited field pointed to
** by Str. It replaces the comma with a a null character and moves the Str
** to the beginning of the next string (skipping white space). Str will
** will point to a null character if no more strings are left. This
** routine is used to fetch strings out of text retrieved from WIN.INI.
*/
static Pchar FetchStr(Pchar& Str)
{
Pchar result = Str;
LPSTR tempStr = Str; // Used because AnsiNext returns an LPSTR;
// Note that we could simply use Str instead
// if we assumed a large data model.
if (Str == NULL)
return result;
while ((*tempStr != '\0') && (*tempStr != ','))
tempStr = AnsiNext(tempStr);
if (*tempStr != '\0')
{
*tempStr = '\0';
tempStr++;
while (*tempStr == ' ')
tempStr = AnsiNext(tempStr);
}
// Change Str to point to next field.
Str += int(tempStr - (LPSTR) Str);
return result;
}
/*
** newstrdup
** Allocate space for and copy a string; allocation is performed
** using 'new' so 'delete' should be used for de-allocation.
*/
static Pchar newstrdup(Pchar S)
{
Pchar P = new char[strlen(S)+1];
strcpy(P, S);
return P;
}
// TPrintout
TPrintout::TPrintout(Pchar ATitle)
{
Title = newstrdup(ATitle);
Banding = FALSE;
ForceAllBands = TRUE;
}
TPrintout::~TPrintout()
{
delete Title;
}
BOOL TPrintout::IsNextPage()
{
return FALSE;
}
// TReplaceStatic
_CLASSDEF(TReplaceStatic)
class TReplaceStatic: public TStatic
{
private:
Pchar Text;
public:
TReplaceStatic(PTWindowsObject AParent, int ResourceId, Pchar AText);
virtual ~TReplaceStatic();
virtual void SetupWindow();
};
TReplaceStatic::TReplaceStatic(PTWindowsObject AParent, int ResourceId,
Pchar AText) : TStatic(AParent, ResourceId, 0)
{
Text = newstrdup(AText);
}
TReplaceStatic::~TReplaceStatic()
{
delete Text;
}
void TReplaceStatic::SetupWindow()
{
char A[80];
char B[80];
TStatic::SetupWindow();
GetText(A, sizeof(A) - 1);
LPSTR C[1];
C[0] = Text;
wvsprintf((LPSTR) B, (LPSTR) A, (LPSTR) C);
SetText(B);
}
// TPrinterAbortDlg
TPrinterAbortDlg::TPrinterAbortDlg(PTWindowsObject AParent, Pchar Template,
Pchar Title, Pchar Device, Pchar Port)
: TDialog(AParent, Template)
{
new TReplaceStatic(this, ID_TITLE, Title);
new TReplaceStatic(this, ID_DEVICE, Device);
new TReplaceStatic(this, ID_PORT, Port);
}
void TPrinterAbortDlg::SetupWindow()
{
TDialog::SetupWindow();
EnableMenuItem(GetSystemMenu(HWindow, FALSE), SC_CLOSE, MF_GRAYED);
}
void TPrinterAbortDlg::WMCommand(RTMessage)
{
UserAbort = TRUE;
}
// TPrinter
/*
** This object type is an encapsulation around the Windows printer
** device interface. After the object is initialized the Status
** field must be checked to see if the object was created correctly.
**
** Examples:
** Creating a default device printing object:
**
** DefaultPrinter = new TPrinter();
**
** Creating a device for a specific printer:
**
** PostScriptPrinter = new TPrinter();
** PostScriptPrinter->SetDevice("PostScript Printer",
** "PSCRIPT", "LPT2:");
**
** Allowing the user to configure the printer:
**
** DefaultPrinter->Configure(MyWindow);
*/
// Initialize the TPrinter object assigned to the default printer
TPrinter::TPrinter()
{
Device = NULL;
Driver = NULL;
Port = NULL;
DeviceModule = 0;
DevSettings = NULL;
Error = 0;
SetDevice(NULL, NULL, NULL); // Associate with default printer
}
// Deallocate allocated resources
TPrinter::~TPrinter()
{
ClearDevice();
}
// Clears the association of this object with the current device
void TPrinter::ClearDevice()
{
if (Device) delete Device;
Device = NULL;
if (Driver) delete Driver;
Driver = NULL;
if (Port) delete Port;
Port = NULL;
if ((int)DeviceModule >= 32) {
FreeLibrary(DeviceModule);
DeviceModule = 0;
}
if (DevSettings)
delete [] (Pchar) DevSettings;
Status = PS_UNASSOCIATED;
}
// GetDefaultPrinter and Equal are helper functions used by
// TPrinter::SetDevice
void TPrinter::GetDefaultPrinter()
{
char Printer[80];
Pchar Cur;
GetProfileString("windows", "device", "", Printer,
sizeof(Printer) - 1);
Cur = Printer;
Device = newstrdup(FetchStr(Cur));
Driver = newstrdup(FetchStr(Cur));
Port = newstrdup(FetchStr(Cur));
}
static BOOL Equal(Pchar S1, Pchar S2)
{
return ((S1 != NULL) && (S2 != NULL) && (strcmp(S1, S2) == 0));
}
/*
Associates the printer object with a new device. If the ADevice
parameter is NULL the Windows default printer is used, otherwise,
the parameters must be ones contained in the [devices] section
of the WIN.INI file.
*/
void TPrinter::SetDevice(Pchar ADevice, Pchar ADriver, Pchar APort)
{
char DriverName[80];
DEVMODE StubDevMode;
if (Equal(Device, ADevice) && Equal(Driver, ADriver) &&
Equal(Port, APort))
return;
ClearDevice();
if (ADevice == NULL)
GetDefaultPrinter();
else {
Device = newstrdup(ADevice);
Driver = newstrdup(ADriver);
Port = newstrdup(APort);
}
Status = PS_OK;
strncpy(DriverName, Driver, sizeof(DriverName) - 1);
strncat(DriverName, ".DRV", sizeof(DriverName) - strlen(DriverName) - 1);
DeviceModule = LoadLibrary(DriverName);
if ((int)DeviceModule < 32)
Status = PS_INVALIDDEVICE;
else {
// Grab the DevMode procedures
ExtDeviceMode = (LPFNDEVMODE) GetProcAddress(DeviceModule, "ExtDeviceMode");
DeviceMode = (PTDeviceModeFcn) GetProcAddress(DeviceModule, "DeviceMode");
if ((DeviceMode == NULL) && (ExtDeviceMode == NULL))
Status = PS_INVALIDDEVICE;
if (ExtDeviceMode != NULL) {
// Get default printer settings
DevSettingSize = ExtDeviceMode(0, DeviceModule, &StubDevMode,
Device, Port, &StubDevMode, NULL, 0);
DevSettings = (PDEVMODE) new char[DevSettingSize];
ExtDeviceMode(0, DeviceModule, DevSettings, Device, Port,
DevSettings, NULL, DM_OUT_BUFFER);
}
else
DevSettings = NULL; // Cannot use local settings
}
}
// Configure brings up a dialog as a child of the given window
// to configure the associated printer driver.
void TPrinter::Configure(PTWindowsObject Window)
{
if (Status == PS_OK)
if (ExtDeviceMode == NULL) // driver only supports DevMode
// if DeviceMode == NULL, Status will != PS_OK
DeviceMode(Window->HWindow, DeviceModule, Device, Port);
else
// Request driver to modify local copy of printer settings
ExtDeviceMode(Window->HWindow, DeviceModule, DevSettings, Device,
Port, DevSettings, NULL, DM_IN_BUFFER | DM_PROMPT | DM_OUT_BUFFER);
}
// Returns a device context for the associated printer, 0 if an
// error occurs or Status is != PS_OK
HDC TPrinter::GetDC()
{
if (Status == PS_OK)
return CreateDC(Driver, Device, Port, (LPSTR) (LPDEVMODE) DevSettings);
else
return 0;
}
static BOOL ProcessDlgMsg(LPMSG PMessage, PTApplication Application)
{
if (Application->KBHandlerWnd && Application->KBHandlerWnd->HWindow)
return IsDialogMessage(Application->KBHandlerWnd->HWindow, PMessage);
else
retur